package cn.da.shuai.cool.system.service.service.impl;

import cn.da.shuai.cool.mybatis.service.impl.CoolServiceImpl;
import cn.da.shuai.cool.system.service.mapper.CoolMenuMapper;
import cn.da.shuai.cool.system.model.entity.menu.CoolMenu;
import cn.da.shuai.cool.system.model.entity.menu.MenuTreeVo;
import cn.da.shuai.cool.system.model.entity.menu.SearchMenuVo;
import cn.da.shuai.cool.system.model.entity.menu.UpdateMenuVo;
import cn.da.shuai.cool.system.service.service.ICoolMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CoolMenuServiceImpl extends CoolServiceImpl<CoolMenuMapper, CoolMenu> implements ICoolMenuService {

    @Override
    public List<MenuTreeVo> getMenuTree(SearchMenuVo vo) {
        // 构建查询条件
        LambdaQueryWrapper<CoolMenu> wrapper = new LambdaQueryWrapper<>();
        if (vo != null) {
            if (StringUtils.hasText(vo.getMenuName())) {
                wrapper.like(CoolMenu::getMenuName, vo.getMenuName());
            }
            if (vo.getMenuType() != null) {
                wrapper.eq(CoolMenu::getMenuType, vo.getMenuType());
            }
        }
        wrapper.orderByAsc(CoolMenu::getOrderNum);

        // 查询所有菜单
        List<CoolMenu> menus = list(wrapper);

        // 转换为树形结构
        return buildMenuTree(menus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createMenu(UpdateMenuVo vo) {
        CoolMenu menu = new CoolMenu();
        BeanUtils.copyProperties(vo, menu);
        save(menu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMenu(UpdateMenuVo vo) {
        CoolMenu menu = new CoolMenu();
        BeanUtils.copyProperties(vo, menu);
        updateById(menu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMenu(Long id) {
        // 检查是否存在子菜单
        LambdaQueryWrapper<CoolMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CoolMenu::getParentId, id);
        long count = count(wrapper);
        if (count > 0) {
            throw new RuntimeException("存在子菜单，无法删除");
        }
        removeById(id);
    }

    private List<MenuTreeVo> buildMenuTree(List<CoolMenu> menus) {
        List<MenuTreeVo> menuTreeList = new ArrayList<>();
        if (menus == null || menus.isEmpty()) {
            return menuTreeList;
        }

        // 转换为MenuTreeVo
        List<MenuTreeVo> menuVoList = menus.stream().map(menu -> {
            MenuTreeVo vo = new MenuTreeVo();
            BeanUtils.copyProperties(menu, vo);
            return vo;
        }).toList();

        // 构建父子关系
        Map<Long, List<MenuTreeVo>> parentMap = menuVoList.stream()
                .filter(menu -> menu.getParentId() != null)
                .collect(Collectors.groupingBy(MenuTreeVo::getParentId));

        // 设置子菜单
        menuVoList.forEach(menu -> menu.setChildren(parentMap.get(menu.getId())));

        // 获取根节点
        return menuVoList.stream()
                .filter(menu -> menu.getParentId() == null)
                .collect(Collectors.toList());
    }
}